package com.example.springbootdemo.accessStatic;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description
 * @Author admin
 * @Date 2020/12/10
 * http://127.0.0.1:8081/show/%E6%98%9F%E7%A9%BA.jpg D盘的图片访问路径
 * 所以图片可以存在服务器上，将show路径映射到本地的静态资源，上传文件存在指定的目录
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${picture.show}")
    private String showPath;
    @Value("${picture.up-path}")
    private String upPath;

    /**
     * 等同于这个配置，通过/s/文件 ----》 访问static-locations
     * 通过showPath/文件 -----》 访问upPath
     *   mvc:
     *     static-path-pattern: /s/**
     *   web:
     *     resources:
     *       static-locations: file:D:/picture/,classpath:/static/,classpath:/templates/
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(showPath).addResourceLocations("file:" + upPath);
    }

}
